﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;

public partial class AccountInfomation_Default : BasePage
{
    private CustomerService _customerService = new CustomerService();
    private Customer _customer;
    private User _user;
    protected void Page_Load(object sender, EventArgs e)
    {
        Initial();
        ShowInfo();
    }

    private void Initial()
    {
        _user = UserLogin;
        if (_user != null)
        {
            if (!_user.IsAdmin)
            {
                hlMainContactInfomation.Visible = false;
                hlEditPassphrase.Visible = false;
            }
            _customer = _user.Customer;
        }

    }

    private void ShowInfo()
    {
        if (_customer != null)
        {
            LiteralMainContact.Text += _customer.CompanyName + "<br />";
            LiteralMainContact.Text += _customer.MainContact.ToString();
            if (_customer.Contacts.Count > 0)
            {
                dtListContacts.DataSource = _customer.Contacts;
                dtListContacts.DataBind();
            }
        }  
        if (_user != null)
        {
            lblUserCurrent.Text = _user.Username;
            LiteralYourContact.Text = _user.ToString();
        }        
    }

    protected void dtListContacts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        Contact contact = _customer.Contacts[e.Item.ItemIndex];
        if (e.CommandName == "EditContact")
        {            
            if (contact != null)
            {
                Session["EditContact"] = contact;
                Response.Redirect("contact.aspx?c="+contact.Id);
            }
        }
        else if (e.CommandName == "DeleteContact")
        {
            if (contact != null && _customer != null && _customer.Contacts.Count > 0)
            {
                _customerService.DeleteContactOfAccount(contact.Id);
                UserLogin.Customer.Contacts.RemoveAt(e.Item.ItemIndex);
                Response.Redirect("Default.aspx");
            }
        }
    }
}